#nullable enable

using System;
using System.IO;
using System.CodeDom.Compiler;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;
using MonoDroid.Tuner;

namespace Xamarin.Android.Tasks
{
	public class GenerateResourceDesignerIntermediateClass : AndroidTask
	{
		public override string TaskPrefix => "GRDIC";

		private const string ResourceDesigner = $"{FixLegacyResourceDesignerStep.DesignerAssemblyNamespace}.Resource";
		private const string ResourceDesignerConstants = $"{FixLegacyResourceDesignerStep.DesignerAssemblyNamespace}.ResourceConstant";

		private const string CSharpTemplate = @"//------------------------------------------------------------------------------
// <auto-generated>
//	This code was generated by a tool. DO NOT EDIT
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.CodeDom.Compiler;

namespace %NAMESPACE% {
	#pragma warning disable IDE0002
	/// <summary>
	/// Android Resource Designer class.
	/// Exposes the Android Resource designer assembly into the project Namespace.
	/// </summary>
	%MODIFIER% partial class Resource : %BASECLASS% {
	}
	#pragma warning restore IDE0002
}
";
		private const string FSharpTemplate = @"//------------------------------------------------------------------------------
// <auto-generated>
//	This code was generated by a tool. DO NOT EDIT
// </auto-generated>
//------------------------------------------------------------------------------
namespace %NAMESPACE%

type %MODIFIER% Resource = %BASECLASS%
";

		public string? Namespace { get; set; }
		public string Modifier { get; set; } = "public";
		public bool IsApplication { get; set; } = false;
		[Required]
		public ITaskItem OutputFile { get; set; } = null!;
		public override bool RunTask ()
		{
			string baseClass = IsApplication ? ResourceDesignerConstants : ResourceDesigner;
			var extension = Path.GetExtension (OutputFile.ItemSpec);
			var language = string.Compare (extension, ".fs", StringComparison.OrdinalIgnoreCase) == 0 ? "F#" : CodeDomProvider.GetLanguageFromExtension (extension);
			//bool isVB = string.Equals (extension, ".vb", StringComparison.OrdinalIgnoreCase);
			bool isFSharp = string.Equals (language, "F#", StringComparison.OrdinalIgnoreCase);
			bool isCSharp = string.Equals (language, "C#", StringComparison.OrdinalIgnoreCase);
			var version = typeof(GenerateResourceDesignerIntermediateClass).Assembly.GetName().Version;
			string template = "";
			if (isCSharp) {
				template = CSharpTemplate.Replace ("%NAMESPACE%", Namespace)
					.Replace ("%BASECLASS%", baseClass)
					.Replace ("%MODIFIER%", Modifier.ToLower ());
			} else if (isFSharp) {
				template = FSharpTemplate.Replace ("%NAMESPACE%", Namespace)
					.Replace ("%BASECLASS%", baseClass)
					.Replace ("%MODIFIER%", Modifier.ToLower ());
			}

			Files.CopyIfStringChanged (template, OutputFile.ItemSpec);
			return !Log.HasLoggedErrors;
		}
	}
}
